package bean.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import Entities.*;


public class ProveedorDAO extends HibernateDAO{
	private static ProveedorDAO instancia;
	private static SessionFactory sf;
	
	public static ProveedorDAO getInstancia(){
		if(instancia == null) {
			setSf((HibernateDAO.getInstancia().getSession().getSessionFactory()));
			instancia = new ProveedorDAO();
		} 
		
		return instancia;
	}
	
	public boolean altaProveedor(Proveedor p ) 
	{
		boolean bandera;
		Session s = HibernateDAO.getInstancia().getSession();
		Transaction t = s.beginTransaction();
		bandera = s.save(p) != null;
		t.commit();
		return bandera;
	}

	@SuppressWarnings("unchecked")
	public List<Proveedor> getProveedores() {
		Session s = HibernateDAO.getInstancia().getSession();
		Transaction t = s.beginTransaction();
		List<Proveedor> lista = s.createQuery("from Proveedor").list();		
		t.commit();
		return lista;
	}

	public Proveedor getProveedor(Integer id) {
		Session s = HibernateDAO.getInstancia().getSession();	
		Transaction t = s.beginTransaction();
		Proveedor p = (Proveedor) s.createQuery(" from Proveedor where id = :id").setInteger("id", id).uniqueResult();;		
		t.commit();
		return p;
	}

	public void actualizarProveedor(Proveedor proveedor) {
		Session s = HibernateDAO.getInstancia().getSession();
		Transaction t = s.beginTransaction();
		String sql1 = " From Proveedor Where id = ?";
		Proveedor p1 = (Proveedor) s.createQuery(sql1).setInteger(0, proveedor.getId()).uniqueResult();
		p1.setNombre(proveedor.getNombre());
		p1.setTelefonoContacto(proveedor.getTelefonoContacto());
		p1.setDeuda(proveedor.getDeuda());
		t.commit();
		
	}
	
	public Carrier getCarrier(int carrierId)
	{
		Session s = HibernateDAO.getInstancia().getSession();
		Transaction t = s.beginTransaction();
	
		Carrier c = (Carrier) s.createQuery("from Carrier where id = ?")
			.setInteger(0, carrierId)
			.uniqueResult();
		
		t.commit();
		
		return c;
	}

	public static void setSf(SessionFactory sf) {
		ProveedorDAO.sf = sf;
	}

	public static SessionFactory getSf() {
		return sf;
	}

	public void bajaProveedor(int id) {
		// TODO Auto-generated method stub
		
	}

	
}
